
package guoan.web.weixin;

import guoan.core.common.WebConfig;
import guoan.core.dto.RestResponse;
import guoan.domain.Train;
import guoan.service.TrainService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.common.base.Strings;

@Controller
@RequestMapping("/weixin/train")
public class WeiXinTrainController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private TrainService trainService;

    /** 跳转到考试主页 */
    @RequestMapping(value = "oauthTrain", method = RequestMethod.GET)
    public String oauthTrain(HttpServletRequest request){
        HttpSession session = request.getSession();
        String openid = (String) session
                .getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
        // 如果教练没绑定过，则去绑定
        if (openid == null) {
            return "error";
        }
        return "/weixin/train/oauthTrain";
    }

    /** 跳转到考试主页 */
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(HttpServletRequest request){
        HttpSession session = request.getSession();
        String openid = (String) session
                .getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);

        // 如果教练没绑定过，则去绑定
        if (trainService.findByOpenid(openid) == null) {
            return "redirect:/weixin/train/oauthTrain";
        }
        return "redirect:/weixin/coach/index";
    }

    @RequestMapping(value = "join", method = RequestMethod.POST)
    public String join(Train train, HttpServletRequest request, Model model){

        if (train == null
                || (train != null && (Strings
                        .isNullOrEmpty(train.getPassword())
                        || Strings.isNullOrEmpty(train.getTrainName()) || Strings
                            .isNullOrEmpty(train.getTrainTel())))) {
            model.addAttribute("msg", "参数错误");
            return "/weixin/msg";
        }
        RestResponse<Boolean> rr = trainService.join(train, request);

        if (rr.getStatus() != 1) {
            model.addAttribute("msg", rr.getMsg());
            return "/weixin/msg";
        } else {
            model.addAttribute("msg", "绑定成功，请等待跳转到预约教练陪练页面");
            model.addAttribute("url", "/weixin/coach/index_2");
            return "/weixin/msg";
        }
    }

    @RequestMapping(value = "update", method = RequestMethod.GET)
    public String toUpdate(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        String openid = (String) session
                .getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
        // 如果教练没绑定过，则去绑定
        Train train = trainService.findByOpenid(openid);
        if (train == null) {
            return "redirect:/weixin/train/oauthTrain";
        } else {
            model.addAttribute("train", train);
        }

        return "/weixin/train/update";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(Train train, HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        String openid = (String) session
                .getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
        // 如果教练没绑定过，则去绑定
        Train trainold = trainService.findByOpenid(openid);
        if (trainold == null) {
            return "redirect:/weixin/train/oauthTrain";
        } else {
            trainold.setCarAddr(train.getCarAddr());
            trainold.setSchoolName(train.getSchoolName());
            trainold.setTrainName(train.getTrainName());
            trainold.setTrainNo(train.getTrainNo());
            trainold.setTrainTel(train.getTrainTel());
            model.addAttribute("train", train);
            trainService.selfUpdate(trainold);
            model.addAttribute("msg", "修改成功");
            model.addAttribute("url", "/weixin/train/update");
            return "/weixin/msg";
        }

    }
}
